<?php
//$loader->requireOnce('includes/EwhValuatePrice/IValuatePriceMethod.class.php');
/**
 *
 * @package modules.dept_pharmacy.includes.PickDM
 * @author viethd
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class PickDMModel{

    private $phaEwhExticketDetailId;
    private $phaEwhExticketId;
    private $phaDrugMaterialId;
    private $reqQuantity;
    private $approveQuantity;
    private $note;
    private $phaReqExticketDetailId;

    private $pickingDetail = array();

    function __construct($phaEwhExticketId, $phaDrugMaterialId,
    $reqQuantity, $approveQuantity, $note, $phaReqExticketDetailId) {

        $this->phaEwhExticketId = $phaEwhExticketId;
        $this->phaDrugMaterialId = $phaDrugMaterialId;
        $this->reqQuantity=$reqQuantity;
        $this->approveQuantity =  $approveQuantity;
        $this->note = $note;
        $this->phaReqExticketDetailId = $phaReqExticketDetailId;        

        //TODO load model data

    }
    public function save() {
        //clean up before save
        $phaEwhExticketDetailObj = Celini::newOrdo('PhaEwhExticketDetail');
        $phaEwhExticketDetailObj->setup($this->phaEwhExticketDetailId);
        $data = array(
            'pha_ewh_exticket_id' => $this->phaEwhExticketId,
            'pha_drug_material_id'=> $this->phaDrugMaterialId,
            'req_quantity' => $this->reqQuantity,
            'approve_quantity' => $this->approveQuantity,
            'note' => $this->note,
            'pha_req_exticket_detail_id' => $this->phaReqExticketDetailId );
        //save model 1
        $phaEwhExticketDetailObj->setData($data);
        $phaEwhExticketDetailObj->persist();
        $this->phaEwhExticketDetailId = $phaEwhExticketDetailObj->get_id();
        //save model detail
        $phaEwhPickDrugObj = Celini::newOrdo('PhaEwhPickDrug');
        $phaEwhPickDrugObj->deleteItemByEDId($this->phaEwhExticketDetailId);
        foreach ($this->pickingDetail as &$item) {
            $dataM1= array(
                'pha_ewh_exticket_detail_id'=>$this->phaEwhExticketDetailId                
            );
            $item->setData($dataM1);
            $item->persist();
            //update remain quantity in import ticket
            $phaEwhImticketDetailObj = Celini::newOrdo('PhaEwhImticketDetail');
            $phaEwhImticketDetailObj->setup($item->get('pha_ewh_imticket_detail_id'));            
            $phaEwhImticketDetailObj->pickDrugMaterial($item->quantity);
        }
        
        

    }
    public function addPickingDetail($phaEwhImticketDetailId, $appQuantity) {
        $phaEwhPickDrugObj = Celini::newOrdo('PhaEwhPickDrug');
        $dataM1= array(                
                'pha_ewh_imticket_detail_id'=>$phaEwhImticketDetailId,
                'quantity'=>$appQuantity
        );
        $phaEwhPickDrugObj->setData($dataM1);
        $this->pickingDetail[] = &$phaEwhPickDrugObj;
    }


}
?>